﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

using YouHP.Utility;
using YouHP.Config;

namespace YouHP.Utility
{
    public class CookiesTools
    {
        /// <summary>
        /// 返回用户密码cookie明文
        /// </summary>
        /// <param name="password">密码密文</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string GetCookiePassword(string password, string key)
        {
            return DES.Decode(password, key);

        }

        /// <summary>
        /// 返回密码密文
        /// </summary>
        /// <param name="password">密码明文</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string SetCookiePassword(string password, string key)
        {
            //			if (password.Length < 32)
            //			{
            //				password = password.PadRight(32);
            //			}
            return DES.Encode(password, key);
        }

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">项</param>
        /// <param name="strValue">值</param>
        public static void WriteUserCookie(string userId, string userName, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["youhpuser"];
            if (cookie == null)
            {
                cookie = new HttpCookie("youhpuser");
            }

            cookie.Values["userid"] = Utils.UrlEncode(userId.ToString());
            cookie.Values["username"] = Utils.UrlEncode(userName.ToString());

            if (expires >= 0)
            {
                cookie.Expires = DateTime.Now.AddMinutes(expires);
            }
            //cookie.Domain = BaseConfig.Instance.DomainName;

            HttpContext.Current.Response.AppendCookie(cookie);
        }

        public static void WriteUserInfoCookie(string namevalue, string passwordvalue, int idvalue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["youhpPreUser"];
            if (cookie == null)
            {
                cookie = new HttpCookie("youhpPreUser");
            }

            cookie.Values["username"] = Utils.UrlEncode(namevalue.ToString());
            cookie.Values["password"] = Utils.UrlEncode(passwordvalue.ToString());
            cookie.Values["userid"] = Utils.UrlEncode(idvalue.ToString());

            if (expires >= 0)
            {
                cookie.Expires = DateTime.Now.AddMinutes(5256000);
            }
            //cookie.Domain = BaseConfig.Instance.DomainName;

            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 清除论坛登录用户的cookie
        /// </summary>
        public static void ClearUserCookie()
        {
            ClearUserCookie("youhpuser");
            ClearUserCookie("youhpPreUser");
        }

        public static void ClearUserCookie(string cookieName)
        {
            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Values.Clear();
            cookie.Expires = DateTime.Now.AddYears(-1);
            cookie.Domain = BaseConfig.Instance.DomainName;

            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        /// <param name="strValue">过期时间(分钟)</param>
        public static void WriteCookie(string strName, string strValue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Value = Utils.UrlEncode(strValue);
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        public static string GetCookie(string strName)
        {
            if (HttpContext.Current.Request.Cookies == null) return "";
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null) return "";
            else
            {
                string value = cookie.Value.ToString();
                return Utils.UrlDecode(value);
            }
            /*
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
            {
                return Utils.UrlDecode(HttpContext.Current.Request.Cookies[strName].ToString());
            }

            return "";
             * */
        }

        public static int GetUserIdCookie()
        {
            return Utils.StrToInt(GetUserCookie("userid"), -1);
        }

        public static string GetUserNameCookie()
        {
            return GetUserCookie("username");
        }
        public static string GetUserPasswordCookie()
        {
            return GetUserCookie("password");
        }

        public static string GetUserCookie(string strName)
        {
            if (HttpContext.Current.Request.Cookies == null) return "";
            HttpCookie cookie = HttpContext.Current.Request.Cookies["youhpPreUser"];
            if (cookie == null) return "";
            object data = cookie[strName];
            if (data == null) return "";
            return Utils.UrlDecode(data.ToString());

            /*
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies["youhpuser"] != null && HttpContext.Current.Request.Cookies["youhp"][strName] != null)
            {
                return Utils.UrlDecode(HttpContext.Current.Request.Cookies["youhpuser"][strName].ToString());
            }

            return "";
            */
        }

        public static string GetPreUserNameCookie(string strName)
        {
            if (HttpContext.Current.Request.Cookies == null) return "";
            HttpCookie cookie = HttpContext.Current.Request.Cookies["youhpPreUser"];
            if (cookie == null) return "";

            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies["youhpPreUser"] != null)
            {
                if (HttpContext.Current.Request.Cookies["youhpPreUser"][strName] != null)
                {
                    return Utils.UrlDecode(HttpContext.Current.Request.Cookies["youhpPreUser"][strName].ToString());
                }
                else
                {
                    return "";
                }
            }

            return "";
        }
        public static string GetPreUserPasswordCookie(string password)
        {
            if (HttpContext.Current.Request.Cookies == null) return "";
            HttpCookie cookie = HttpContext.Current.Request.Cookies["youhpPreUser"];
            if (cookie == null) return "";

            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies["youhpPreUser"] != null)
            {
                if (HttpContext.Current.Request.Cookies["youhpPreUser"][password] != null)
                {
                    return Utils.UrlDecode(HttpContext.Current.Request.Cookies["youhpPreUser"][password].ToString());
                }
                else
                {
                    return "";
                }
            }

            return "";
        }

        public static string GetPreUserIDCookie(string id)
        {
            if (HttpContext.Current.Request.Cookies == null) return "";
            HttpCookie cookie = HttpContext.Current.Request.Cookies["youhpPreUser"];
            if (cookie == null) return "";

            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies["youhpPreUser"] != null)
            {
                if (HttpContext.Current.Request.Cookies["youhpPreUser"][id] != null)
                {
                    return Utils.UrlDecode(HttpContext.Current.Request.Cookies["youhpPreUser"][id].ToString());
                }
                else
                {
                    return "";
                }
            }

            return "";
        }
     
        //public static void WriteUserNameCookie(string userName)
        //{
        //    WriteCookie("youhpPreUser", userName, 5256000);    //5256000 minutes  永久有效
        //}

        //public static void WriteUserPasswordCookie(string password)
        //{
        //    WriteCookie("youhpPreUser", password, 5256000);
 
        //}

        /// <summary>
        /// 从cookie中获取转向页
        /// </summary>
        /// <returns></returns>
        public static string GetPreUrl()
        {
            if (GetCookie("youhppreurl").Length <= 0)
            {
                return "index.aspx";
            }
            else
            {
                return GetCookie("youhppreurl");
            }
        }

        /// <summary>
        /// 设置cookie中获取转向页
        /// </summary>
        /// <returns></returns>
        public static void SetPreUrl(string url)
        {
            WriteCookie("youhppreurl", url, 5256000);    //5256000 minutes == 永久有效
        }
    }
}
